﻿Public Class Idade
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    End Sub

    Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        If IsDate(Me.TextBox1.Text) Then
            Me.Label1.Text = Me.CalculaIdade(Me.TextBox1.Text)
        End If
    End Sub

    Private Function CalculaIdade(dataNasc As Date) As Integer
        Dim mes As String = Now.Month
        Dim dia As String = Now.Day
        Dim Idade As Integer

        If mes > dataNasc.Month Or (mes = dataNasc.Month And dia > dataNasc.Day) Then
            Idade = Now.AddYears(-dataNasc.Year).Year
        Else
            Idade = Now.AddYears(-(dataNasc.Year + 1)).Year
        End If
        Return Idade
    End Function

End Class